package com.zscat.mallplus.center.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.center.BaseController;
import com.zscat.mallplus.center.entity.CenterAppt;
import com.zscat.mallplus.center.service.ICenterApptService;
import com.zscat.mallplus.center.utils.StringUtils;
import com.zscat.mallplus.util.EasyPoiUtils;
import com.zscat.mallplus.utils.CommonResult;

@RestController
@RequestMapping("/center/appt")
public class CenterApptController extends BaseController {

	@Resource
	private ICenterApptService centerApptService;

	@GetMapping
	public Object add(CenterAppt centerAppt) {
		centerAppt.setCreateTime(new Date());
		return toAjax(centerApptService.save(centerAppt));
	}

	/**
	 * 门票预约列表
	 * 
	 * @param centerAppt
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("list")
	public Object list(CenterAppt centerAppt, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
		if (StringUtils.isEmpty(centerAppt.getBeginTime())) {
			return new CommonResult().success(
					centerApptService.page(new Page<CenterAppt>(pageNum, pageSize), new QueryWrapper<>(centerAppt)));
		} else {
			String createTime = centerAppt.getBeginTime() + " 00:00:00";
			String endTime = centerAppt.getBeginTime() + " 23:59:59";
			return new CommonResult().success(centerApptService.page(new Page<CenterAppt>(pageNum, pageSize),
					new QueryWrapper<>(centerAppt)
							.and(StringUtils.isNotEmpty(centerAppt.getBeginTime()),
									wrapper -> wrapper.ge("create_time", createTime))
							.and(StringUtils.isNotEmpty(centerAppt.getBeginTime()),
									wrapper -> wrapper.le("create_time", endTime))));
		}
	}

	@GetMapping("/exportExcel")
	@CrossOrigin
	public void export(HttpServletResponse response, CenterAppt entity) {
		// 模拟从数据库获取需要导出的数据
		List<CenterAppt> personList = centerApptService.list(new QueryWrapper<>(entity));
		// 导出操作
		EasyPoiUtils.exportExcel(personList, "导出H5预约数据", "H5预约", CenterAppt.class, "导出数据.xls", response);
	}

	@GetMapping("exportList")
	public Object list(CenterAppt centerAppt) {
		return new CommonResult().success(centerApptService.list(new QueryWrapper<>(centerAppt)));
	}
}
